package com.parking.status.sync.netty.entity;

import com.parking.status.sync.entity.ParkingLot;
import com.parking.status.sync.netty.util.LocalInfo;
import com.parking.status.sync.repository.ParkingLotRepository;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.Date;

/**
 * Created by Administrator on 2015/9/16.
 */
public class ContentParkingLot extends ContentBase {

    @Autowired
    ParkingLotRepository repo;

    private short totallot;

    private short monthly_total;

    private short hourly_total;

    private short total_left;

    private short monthly_left;

    private short hourly_left;

    public ContentParkingLot() {}

    public ContentParkingLot(short totallot, short monthly_total, short hourly_total,
                             short total_left, short monthly_left, short hourly_left) {
        this.totallot = totallot;
        this.monthly_total = monthly_total;
        this.hourly_total = hourly_total;
        this.total_left = total_left;
        this.monthly_left = monthly_left;
        this.hourly_left = hourly_left;
    }

    public short getTotallot() {
        return totallot;
    }

    public void setTotallot(short totallot) {
        this.totallot = totallot;
    }

    public short getMonthly_total() {
        return monthly_total;
    }

    public void setMonthly_total(short monthly_total) {
        this.monthly_total = monthly_total;
    }

    public short getHourly_total() {
        return hourly_total;
    }

    public void setHourly_total(short hourly_total) {
        this.hourly_total = hourly_total;
    }

    public short getTotal_left() {
        return total_left;
    }

    public void setTotal_left(short total_left) {
        this.total_left = total_left;
    }

    public short getMonthly_left() {
        return monthly_left;
    }

    public void setMonthly_left(short monthly_left) {
        this.monthly_left = monthly_left;
    }

    public short getHourly_left() {
        return hourly_left;
    }

    public void setHourly_left(short hourly_left) {
        this.hourly_left = hourly_left;
    }

    public byte getContentType() {
        return MessageType.PARKINGLOT;
    }

    @Override
    public byte[] serialize() {
        byte[] bytes = null;
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = null;
        InputStream bis = null;
        ObjectInputStream ois = null;
        int contentlength = 0;
        try {
            oo = new ObjectOutputStream(bo);
            oo.writeShort(totallot);
            contentlength += 2;
            oo.writeShort(monthly_total);
            contentlength += 2;
            oo.writeShort(hourly_total);
            contentlength += 2;
            oo.writeShort(total_left);
            contentlength += 2;
            oo.writeShort(monthly_left);
            contentlength += 2;
            oo.writeShort(hourly_left);
            contentlength += 2;
            oo.flush();
            bytes = bo.toByteArray();
            //再读一下去掉了头信息
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            bytes = new byte[contentlength];
            ois.readFully(bytes);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return (bytes);
    }

    @Override
    public ContentBase deserialize(byte[] contentBytes) {
        ByteBuf buf = Unpooled.copiedBuffer(contentBytes);
        this.totallot = buf.readShort();
        this.monthly_total = buf.readShort();
        this.hourly_total = buf.readShort();
        this.total_left = buf.readShort();
        this.monthly_left = buf.readShort();
        this.hourly_left = buf.readShort();
        return this;
    }

    @Override
    public void save() {
        ParkingLot parkinglot = new ParkingLot();
        parkinglot.setIdPark(LocalInfo.getIdParking());
        parkinglot.setTimeCreate(new Date());
        parkinglot.setTotal_lot(totallot);
        parkinglot.setTotal_monthly(monthly_total);
        parkinglot.setTotal_hourly(hourly_total);
        parkinglot.setLeft_lot(total_left);
        parkinglot.setLeft_monthly(monthly_left);
        parkinglot.setLeft_hourly(hourly_left);
        repo.saveAndFlush(parkinglot);
    }
}
